using System;
using System.Text;
using Sinacor.OMM.Translator.Interfaces;
using System.Xml;
using System.Globalization;

namespace Sinacor.OMM.Translator.Translators.STM
{
    public class MessageAN53 : BaseTranslator, ITranslator
    {
        private static MessageAN53 _instance = null;

        private MessageAN53()
        {
        }

        public static MessageAN53 GetInstance()
        {
            if (_instance == null)
                _instance = new MessageAN53();

            return _instance;
        }

        #region ITranslator Members

        public Sinacor.OMM.MessageEntity.BaseMessage TranslateToRouterMessage()
        {
            return TranslateToMessage();
        }

        public Sinacor.OMM.MessageEntity.BaseMessage TranslateToProcessMessage()
        {
            throw new System.NotImplementedException();
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Sinacor.OMM.MessageEntity.BaseMessage TranslateToMessage()
        {
            Sinacor.OMM.MessageEntity.STM.MessageAN53 _message = new Sinacor.OMM.MessageEntity.STM.MessageAN53();
            MessageEntity.BaseMessage baseMessage;

            _message.CblcIdentificator = Sinacor.OMM.MessageEntity.Enum.MessagesCBLCSource.AN53;
            _message.Source = Sinacor.OMM.MessageEntity.Enum.MessageSource.CBLC;
            _message.Original = OriginalMessage.Body;
            _message.System = "PAP";

            baseMessage = TranslateMessageLabel(OriginalMessage.Label);
            _message.SequentialID = baseMessage.SequentialID;
            _message.ID = baseMessage.ID;
            _message.OperationDate = GetOperationDate(_message.ID);

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(OriginalMessage.Body);
            XmlNode node = xmlDoc.SelectNodes("//AN53")[0];

            _message.COD_ESP = node.SelectSingleNode("COD_ESP").InnerText;
            _message.COD_ISIN = node.SelectSingleNode("COD_ISIN").InnerText;
            _message.COD_NEG = node.SelectSingleNode("COD_NEG").InnerText.Trim();
            _message.COD_NEG_PAP_OBJT = node.SelectSingleNode("COD_NEG_PAP_OBJT").InnerText.Trim();
            _message.COD_SIST_NEG = Convert.ToInt32(node.SelectSingleNode("COD_SIST_NEG").InnerText);

            if (node.SelectSingleNode("COD_TIPO_MERC") != null && !string.IsNullOrEmpty(node.SelectSingleNode("COD_TIPO_MERC").InnerText))
                _message.COD_TIPO_MERC = Convert.ToInt32(node.SelectSingleNode("COD_TIPO_MERC").InnerText);

            if (node.SelectSingleNode("DATA_INIC_NEG_ATIV_MERC") != null && !string.IsNullOrEmpty(node.SelectSingleNode("DATA_INIC_NEG_ATIV_MERC").InnerText))
                _message.DATA_INIC_NEG_ATIV_MERC = Convert.ToDateTime(node.SelectSingleNode("DATA_INIC_NEG_ATIV_MERC").InnerText);

            if (node.SelectSingleNode("DATA_LIM_NEG_ATIV_MERC") != null && !string.IsNullOrEmpty(node.SelectSingleNode("DATA_LIM_NEG_ATIV_MERC").InnerText))
                _message.DATA_LIM_NEG_ATIV_MERC = Convert.ToDateTime(node.SelectSingleNode("DATA_LIM_NEG_ATIV_MERC").InnerText);

            if (node.SelectSingleNode("DATA_VENC") != null && !string.IsNullOrEmpty(node.SelectSingleNode("DATA_VENC").InnerText))
                _message.DATA_VENC = Convert.ToDateTime(node.SelectSingleNode("DATA_VENC").InnerText);

            if (node.SelectSingleNode("FAT_COT_PREC") != null && !string.IsNullOrEmpty(node.SelectSingleNode("FAT_COT_PREC").InnerText))
                _message.FAT_COT_PREC = Convert.ToInt32(node.SelectSingleNode("FAT_COT_PREC").InnerText);

            _message.NOME_PREG = node.SelectSingleNode("NOME_PREG").InnerText.Trim();
            _message.NUM_DIST = Convert.ToInt32(node.SelectSingleNode("NUM_DIST").InnerText);

            if (node.SelectSingleNode("NUM_SERI") != null && !string.IsNullOrEmpty(node.SelectSingleNode("NUM_SERI").InnerText))
                _message.NUM_SERI = Convert.ToInt32(node.SelectSingleNode("NUM_SERI").InnerText);

            if (node.SelectSingleNode("QTE_MINI_LOTE_PAD") != null && !string.IsNullOrEmpty(node.SelectSingleNode("QTE_MINI_LOTE_PAD").InnerText))
                _message.QTE_MINI_LOTE_PAD = Convert.ToInt32(node.SelectSingleNode("QTE_MINI_LOTE_PAD").InnerText);

            if (node.SelectSingleNode("VAL_PREC_EXER") != null && !string.IsNullOrEmpty(node.SelectSingleNode("VAL_PREC_EXER").InnerText))
                _message.VAL_PREC_EXER = Convert.ToDecimal(node.SelectSingleNode("VAL_PREC_EXER").InnerText);

            return _message;
        }
    }
}